package org.wx.ormtest.step02;

import lombok.extern.slf4j.Slf4j;
import org.wx.ormtest.init.PropertiesHelper;
import org.wx.ormtest.step00.entity.User;
import org.wx.ormtest.step02.helper.UserEntityHelper;

import java.sql.*;

/**
 * @description: 利用反射+注解进行结果集映射
 * @author: wangxun
 * @createDate: 2021/3/2
 * @version: 1.0
 */
@Slf4j
public class App02 implements Runnable{
    private final static String DRIVERCLASS = PropertiesHelper.get("driverClass");
    private final static String URL =  PropertiesHelper.get("url");
    private final static String USERNAME = PropertiesHelper.get("userName");
    private final static String PASSWORD =  PropertiesHelper.get("password");



    @Override
    public void run() {
        long startTime = System.currentTimeMillis();
        log.info("进入App02初始化方法...{}",startTime);
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            //SPI注册驱动
            Class.forName(DRIVERCLASS);
            // 连接
            connection = DriverManager.getConnection(URL,USERNAME,PASSWORD);
            // 查询前1w条
            statement = connection.prepareStatement("select * from orm.t_user limit 300000");

            ResultSet rs = statement.executeQuery();
            while (rs.next()){
                UserEntityHelper.create(rs);
            }

            log.info("执行完毕，总共花费：{}",(System.currentTimeMillis()-startTime));
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                statement.close();
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        App02 app02 = new App02();
        new Thread(app02).start();
    }
}
